
package teehee.Form;

import android.content.Context;
import teehee.Action.WodPageAction;
import teehee.DB.DB;

/**
 * A class that contains static methods to validate the WOD Page.  Returns booleans for each method.
 * 
 * @see teehee.UI.WodPage
 * @author Alec
 */

public class WodPageForm 
{
	/**
	 * Checks if a time is entered appropriately.  Acceptable formats are ":ss", "mm:ss", "hh:mm:ss".  A single time value may not exceed 60; max value @ 59:59:59.
	 * 
	 * @param t The String to check
	 * 
	 * @return True if the above conditions are met
	 */
	public static boolean validateTime(String t)
	{
		t = t.trim();
		if(!(t.matches (":\\d\\d") || t.matches("(\\d\\d:){1,2}?(\\d\\d)")))
		   return false;

		for(String s : t.split(":"))
			if((s = s.trim()).length() > 0)
			   if(!(s.matches("\\d+?") && Integer.parseInt(s) < 60))
				 return false;
		return true;
	}
	
	/**
	 * Checks if reps are entered appropriately.  Acceptable formats are empty string, 'dd', 'dd-dd', 'dd-dd-dd',...
	 * @param s The String to check
	 * 
	 * @return True if the above conditions are met
	 */
	
	public static boolean validateRepsField(String s)
	{
	  if ((s = s.trim()).length() == 0)
		  return true;
	  
	  if(s.startsWith("-") || s.endsWith("-"))
		  return false;
	  
	  for(String x : s.split("-"))
		if(!x.matches("\\d+?"))
			return false;
	  return true;
	  
	}
	
	/**
	 * Checks if a generic string field is valid (e.g. cannot contain random-ass characters)
	 * 
	 * @param s The String to check
	 * 
	 * @return True if the string was not blank space.
	 */
	
	public static boolean validateGeneralField(String s)
	{
		return s.matches("(\\w|\\s|,)*?");
	}
	
	/**
	 * Checks to see if the weight is a decimal value
	 * 
	 * @param w The String object to check
	 * 
	 * @return True if the weight value is a number
	 */
	public static boolean validateWeight(String w)
	{
		return w.trim().matches("\\d+?");
	}
	
	public static DB processInput(String exercise)
	{
		String ex = WodPageAction.convertToDbFormat(exercise);
		return WodPageAction.find(ex);
	}
	
	public static boolean createEntry(Context ctx, String date, String reps, String time, String weights, String comments, DB entry)
	{
		return WodPageAction.createEntry(ctx,date, reps, time, weights,comments,entry);
	}
}
